package top.techqi.usbd.finder

import android.hardware.usb.UsbDevice
import android.util.SparseArray
import top.techqi.usbd.UsbDriver
import top.techqi.usbd.UsbFinder
import top.techqi.usbd.driver.serial.Wch341SerialDriver

object Wch341Finder : UsbFinder {
    private const val VENDOR_ID = 0x1A86
    private const val PRODUCT_ID_CH340 = 0x7523
    private const val PRODUCT_ID_CH341 = 0x5523

    override fun detectDrivers(
        device: UsbDevice, remains: MutableSet<Int>,
        drivers: SparseArray<Pair<String, Class<out UsbDriver>>>
    ): Int {
        when (device.vendorId) {
            VENDOR_ID -> when (device.productId) {
                PRODUCT_ID_CH340 -> {
                    remains.clear()
                    drivers.put(
                        device.getInterface(0).id,
                        Pair("CH340", Wch341SerialDriver::class.java)
                    )
                    return 1
                }

                PRODUCT_ID_CH341 -> {
                    remains.clear()
                    drivers.put(
                        device.getInterface(0).id,
                        Pair("CH341", Wch341SerialDriver::class.java)
                    )
                    return 1
                }
            }
        }
        return 0
    }
}
